﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace StreamingTransmit
{
    public partial class frm_TransmitServer : Form
    {
        //服务器启动成功标志
        int startSuccess = 0;
        //侦听端口
        short port = 556;
        public frm_TransmitServer()
        {
            InitializeComponent();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            DateTime dt = DateTime.Now;
            label3.Text = dt.ToLongTimeString();
            label4.Text = dt.ToShortDateString();
            label2.Text = Convert.ToString(Hikmrserver.MR_GetCurConnection());
        }

        private void frm_TransmitServer_Load(object sender, EventArgs e)
        {
            /*摘自：http://zhidao.baidu.com/link?url=i_U6KD6kaeBysDI5B4qhHvgKS3bepP6Tld18qxKOw1uCBXrYpDZ-3NF-X7gxkwnTNVyIITHF7BFF2CcwbOxRw_
             */
            //判断该程序是否已经启动，只能启动一个实例
            if (System.Diagnostics.Process.GetProcessesByName("StreamingTransmit").ToList().Count > 0)
            {
                //存在
                MessageBox.Show("该程序已经运行");
                Application.Exit();
            }
            else
            {
                //不存在 
            }  


            //初始化
            if (Hikmrserver.MR_InitStreamServerLib() == 0)
            {
                startSuccess = 1;
                btnStart.Enabled=false;
                MessageBox.Show("初始化成功");
            }
            timer1.Start();
            timer2.Start(); 
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (Hikmrserver.MR_StartServer(port) == 0)
            {
                startSuccess = 1;
                btnStart.Enabled = false;
            }
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            if (Hikmrserver.MR_StopServer() == 0)
            {
                startSuccess = 0;
                btnStart.Enabled = true;
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            if (startSuccess == 1)
            {
                MessageBox.Show("请先停止服务");
                return;
            }
            else 
            {
                if (Hikmrserver.MR_FiniStreamSerLib()== 0)
                {  
                    //Application.Exit();
                }
                else { MessageBox.Show("关闭程序失败,请重新停止服务"); return; }
            }
        }

        private void frm_TransmitServer_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (startSuccess == 1)
            {
                MessageBox.Show("请先停止服务");
                return;
            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            label2.Text = Convert.ToString(Hikmrserver.MR_GetCurConnection());
        }
    }
}
